#ifndef IFCFG_H_
#define IFCFG_H_
 
#include <iostream>
#include <fstream>
#include <string>
#include "cmd.h"
#include "access_point.h"
#include "bootproto.h"

extern std::string tempname;

class IfCfg
{
 public:
  
  IfCfg(std::string fname, std::string adaptor, 
	AccessPoint *ap, BootProto *bp) 
    : __fname(fname), __adaptor(adaptor), __ap(ap), __bp(bp)
  { } 

  virtual int out()
  {         
    
    std::ofstream cfg_file(__fname.c_str());
    if(cfg_file.bad())
      {
	std::cerr<< "cout not open " <<  __fname << " for output" << std::endl;
	std::cerr << "Exiting." << std::endl;
	return -1;
      }
      // file is ok . output our header
    cfg_file << "#"
	     << __adaptor 
	     <<  " Config File - used to configure the wireless interface " 
	     << std::endl
	     << "# Warning: This file is auto-generated by wnetscan" 
	     << std::endl
	     << "# if you make changes SAVE THEM" 
	     << std::endl;
    
    // device line
    cfg_file << "DEVICE=" << __adaptor << std::endl;
    
      // on boot line
    cfg_file << "ONBOOT=yes" << std::endl;
    
    // Type line
    cfg_file << "TYPE=Wireless" << std::endl;
    
    // hwaddr line
    Cmd mac_addr("ifconfig",__adaptor,"| grep HWaddr >",tempname);	
    // mac_addr.echo();
    mac_addr.exec();
    std::ifstream mac_file(tempname.c_str());
    if(mac_file.bad())
      {
	std::cerr << "Temp File Not Open" << std::endl;
      }
    else
      {
	std::string str;
	bool gotit=false;
	
	while (!mac_file.eof() && mac_file.good())
	  {
	    if(gotit)
	      break;
	    getline(mac_file,str);
	    // std::cout << "MAC: " << str << std::endl;
	    unsigned pos=str.find("HWaddr");
	    if(pos!=std::string::npos)
	      {
		pos+=7;
		const int mac_len=17;
		std::string mac_address=str.substr(pos,mac_len);
		// std::cout << "Mac Address=" << mac_address << std::endl;
		cfg_file << "HWADDR=" << mac_address <<  std::endl;
		gotit=true;
		break;
	      }
	  }
      }
    
    unlink(tempname.c_str());
    
    cfg_file << "ONBOOT=yes" << std::endl;
    
    if(__ap->has_essid())
      cfg_file << "ESSID="<< __ap->get_essid() << std::endl;
    
    //     this seems liks it woulf be useful for NWID or access point name
    //      if(__ap->has_address())
    //	cfg_file << "address=" << __ap->get_address() << std::endl;
    
    
    // note AP mode should be "Master" but then adaptor needs to be 
    // Managed.  other modes are TBD

    cfg_file << "MODE=Managed" << std::endl;
    
    // Channel line - this is really the most important one.
    if(__ap->has_channel())
      cfg_file << "CHANNEL=" << __ap->get_channel() << std::endl;
    
    cfg_file << "PERRDNS=yes"<<  std::endl;
    
    // this should work on recent cards.
    cfg_file << "RATE=auto"<<  std::endl;
    
    // Coming soon --
    //if(__ap->has_secure())
    //{
    // if(__ap->has_key())
    // cfg_file << "key=" << __ap->get_key() << std::endl; 
    //}

    __bp->output(cfg_file);

    return 0;
  }

  virtual int echo()
    {
    
      // file is ok . output our header
      std::cout << std::endl
		<< "#-------------------------------------------------------"	
		<< std::endl
		<< "# "
		<< std::endl
		<< "# "
		<< __adaptor 
		<<  " Config File - This is what wnetscan would write"
		<< std::endl
		<< "# to the wireles adaptor configuration file"
		<< std::endl
		<< "# "
		<< std::endl;

      // "device" line
      std::cout << "DEVICE=" << __adaptor << std::endl;
	
      // "on boot" line
      std::cout << "ONBOOT=yes" << std::endl;
      
    // "Type" line
      std::cout << "TYPE=Wireless" << std::endl;
    
    // "hwaddr" line
    Cmd mac_addr("ifconfig",__adaptor,"| grep HWaddr >",tempname);	
    mac_addr.exec();
    std::ifstream mac_file(tempname.c_str());
    if(mac_file.bad())
      {
	std::cerr << "Temp File Not Open" << std::endl;
      }
    else
      {
	std::string str;
	bool gotit=false;
	
	while (!mac_file.eof() && mac_file.good())
	  {
	    if(gotit)
	      break;
	    getline(mac_file,str);
	    // std::cout << "MAC: " << str << std::endl;
	    unsigned pos=str.find("HWaddr");
	    if(pos!=std::string::npos)
	      {
		pos+=7;
		const int mac_len=17;
		std::string mac_address=str.substr(pos,mac_len);
		// std::cout << "Mac Address=" << mac_address << std::endl;
		std::cout << "HWADDR=" << mac_address <<  std::endl;
		gotit=true;
		break;
	      }
	  }
      }

	unlink(tempname.c_str());
    
      // boot proto   - DHCP seems to be broken but that is 
      //    what we should use
    std::cout << "ONBOOT=yes" << std::endl;
    
    if(__ap->has_essid())
      std::cout << "ESSID="<< __ap->get_essid() << std::endl;
    
    //     this seems liks it woulf be useful for NWID or access point name
    //      if(__ap->has_address())
    //	std::cout << "address=" << __ap->get_address() << std::endl;
    
    
    // note AP mode should be "Master" but then adaptor needs to be 
    // Managed.  other modes are TBD
    // if(__ap->has_mode())
    // std::cout << "mode:       "    << __ap->get_mode() << std::endl;
    std::cout << "MODE=Managed" << std::endl;
    
    // Channel line - this is really the most important one.
    if(__ap->has_channel())
      std::cout << "CHANNEL=" << __ap->get_channel() << std::endl;
    
    // these are a little questionable
    std::cout << "PERRDNS=yes"<<  std::endl;
    
    // this should work on recent cards.
    std::cout << "RATE=auto"<<  std::endl;
    
    // Coming soon --
    //if(__ap->has_secure())
    //{
    // if(__ap->has_key())
    // std::cout << "key=" << __ap->get_key() << std::endl; 
    //}
    __bp->echo();
    std::cout << std::endl;
    
    return 0;
  }
  virtual ~IfCfg()
    {
    }
  
 private:
  std::string __fname;
  std::string __adaptor;
  AccessPoint *__ap;
  BootProto *__bp;
};


#endif



